﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmEpSoTheBHYT : FormBase
    {
        public frmEpSoTheBHYT()
        {
            InitializeComponent();
            //
            Init_Control();
        }

        public override void Show_Form()
        {
            this.Show();
        }

        DataGridHandle m_GridHandle;
        protected override void Init_Control()
        {
            //Init Grid
            dgvDSThe.Columns.Clear();
            //Khởi tạo danh sách cho grid
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("sobhxh", "sobhxh", 80));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("sokcb", "sokcb", 120));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("hoten", "hoten", 120));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ngaysinh", "ngaysinh", 90, Globals.CS_DISPLAY_DATETIME_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("gioitinh", "gioitinh", 70));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ma_tinh", "ma_tinh", 70));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ma_bv", "ma_bv", 70));
            FormGlobals.Grid_Init(dgvDSThe, arrColumn, "", 1);
        }

        DataTable dt = new DataTable();
        private void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "Excel file (*.xls)|*.xls";

                if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    FormGlobals.App_ProgressShow(delegate()
                    {
                        dt = ExcelHelper.Excel_GetData(openFileDialog1.FileName, true, 0, false);
                        dgvDSThe.AutoGenerateColumns = false;
                        dgvDSThe.DataSource = dt;
                    }, "Đọc dữ liệu");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count == 0 || !FormGlobals.Message_Confirm("Bạn có chắc muốn cập nhật mã thẻ y tế trong đơn vị"))
                return;
            //
            try
            {
                string soSoBHXH,sokcb,maTinh,maBV ="";
                int counter = 0;
                FormGlobals.App_ProgressShow(delegate()
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        soSoBHXH = Globals.Object_GetValue<string>(dr["sobhxh"]);
                        sokcb = Globals.Object_GetValue<string>(dr["sokcb"]);
                        maTinh = Globals.Object_GetValue<string>(dr["ma_tinh"]);
                        maTinh = maTinh !="" ? maTinh : Program.strRegionCode;
                        maBV = Globals.Object_GetValue<string>(dr["ma_bv"]);
                        //
                        if(Employee_Controller.Instance().CapNhatSoTheBHYT(Program.intBranchID, soSoBHXH,sokcb,maTinh,maBV))
                            counter ++;
                    }
                }, "Đang cập nhật dữ liệu...");
            //
                string strMessage = "Cập nhật thành công " + counter.ToString() + " nhân sự";
                MessageBox.Show(strMessage, "Thông báo", MessageBoxButtons.OK);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
